Skip to content

Make loopback.rest self-contained - #167

Closed
bajtos wants to merge 2 commits into
masterfrom
feature/include-token-in-rest-middleware
Closed

bajtos wants to merge 2 commits into
masterfrom
feature/include-token-in-rest-middleware

Conversation

@bajtos

@bajtos bajtos commented Feb 4, 2014

Copy link
Copy Markdown
Member

The first commit implements app.getModelByType, which is a twin of loopback.geModelByType, except it considers only models added via app.model.

The second commit amends loopback.token and loopback.rest middleware to allow loopback.rest to add access token lookup to all requests when the app has authentication enabled.

Once this patch is accepted, there will be two main ways of setting up a LB app:

/** 1. REST server only **/
var app = loopback();
// setup models & datasources
app.enableAuth();

// optionally use favicon, logger, cookieParser
app.use(loopback.rest());
app.listen();

/** 2. Web app with REST API **/
var app = loopback();
// setup models & datasources
app.enableAuth();

app.use(loopback.favicon());
app.use(loopback.logger('dev'));
// optionally use cookieParser
app.use(loopback.bodyParser());
app.use('/api', loopback.rest());
app.use(app.router);
app.use(loopback.static(__dirname + '/public'));
app.use(loopback.urlNotFound());
app.use(loopback.errorHandler());
app.listen();

Although loopback.rest internally calls loopback.token middleware, it's possible to install a custom loopback.token instance. This works because loopback.token does not override the token already present in the request.

var app = loopback();
// setup models & datasources
app.enableAuth();

app.use(loopback.token({ model: MyToken }));
app.use(loopback.rest());
app.listen();

/to @ritch @raymondfeng please review. This patch is one of the steps on the path towards a better & smaller app.js template in loopback-workspace.

@slnode

slnode commented Feb 4, 2014

Copy link
Copy Markdown

Test PASSed. To trigger a build add comment - ".test\W+please"
Refer to this link for build results: http://ci.strongloop.com/job/loopback/885/

@bajtos

bajtos commented Feb 4, 2014

Copy link
Copy Markdown
Member Author

@ritch and I had a long chat about the model dependencies. We have agreed that f07446a is a band-aid, a workaround for the underlying fundamental problem. We need to come up with a better way how to resolve model dependencies, so that it's easier to write unit-tests for loopback-based code.

I am closing the pull request, we can reopen it once we have solved the problem of model dependency resolution.

@bajtos bajtos closed this Feb 4, 2014
@bajtos bajtos reopened this May 16, 2014
@slnode

slnode commented May 16, 2014

Copy link
Copy Markdown

Test Failed. To trigger a build add comment - ".test\W+please"
Refer to this link for build results: http://ci.strongloop.com/job/loopback/1205/

Miroslav Bajtoš added 2 commits May 16, 2014 15:04
A method similar to loopback.getModelByType(), but checking
only the models added to the app via `app.model`.
Make `loopback.rest` self-contained, so that authentication works
out of the box.

    var app = loopback();
    app.enableAuth();
    app.use(loopback.rest());

Note that cookie parsing middleware is not added, users have to
explicitly configure that if they want to store access tokens
in cookies.

Modify `loopback.token` to skip token lookup when the request already
contains `accessToken` property. This is in line with other
connect-based middleware like `cookieParser` or `json`.
@slnode

slnode commented May 16, 2014

Copy link
Copy Markdown

Test Failed. To trigger a build add comment - ".test\W+please"
Refer to this link for build results: http://ci.strongloop.com/job/loopback/1206/

@bajtos bajtos closed this May 16, 2014
@bajtos
bajtos deleted the feature/include-token-in-rest-middleware branch June 5, 2014 07:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants